package com.pms.actions;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

//import org.apache.log4j.Logger;

public class MailUtil{
	
	//private static Logger logger = Logger.getLogger("MailUtil.class");
	
	public void sendMail(String from, String to, String subject, String body)throws Exception{
		System.out.println("InSide sendMail of MailUtil");
		System.out.println("------------------------------------------ send mail is calling ------------------------");
		
		String host="smtp.gmail.com";
		Properties props = System.getProperties();
	    props.put("mail.smtp.starttls.enable","true");
	    props.put("mail.smtp.auth","true");
	    props.put("mail.smtp.host", host);
		
		Session session = Session.getInstance(props, null);
		try {
		
			Message msg = new MimeMessage(session);
			msg.setFrom(new InternetAddress(from));
			InternetAddress[] address = {new InternetAddress(to)};
			msg.setRecipients(Message.RecipientType.TO, address);
			msg.setSubject(subject);
			msg.setSentDate(new java.util.Date());
			msg.setHeader("x-priority", "1");
			msg.setHeader("x-msmail-priority", "high");
			msg.setText(body);
			msg.setContent(body, "text/html");
			
			Session session2 = Session.getInstance(props, null);
	        
	        Transport transport = session2.getTransport("smtp");
	        transport.connect(host, "notify@blinktechnologies.net", "blinktech");
	        transport.sendMessage(msg, address);
	        transport.close();
	        
	        System.out.println("--------------------------Body of the Mail is -----------");
	        System.out.println(body);
	        System.out.println("--------------------------Email Sent successfully-----------");
		}catch (Exception e){
			throw e;
		}
	}
	
	public static void setHTMLContent(Message msg, String message)throws Exception {
		String html = "<html><body>";
		html +=message.replaceAll("\"","&apos;");
		html += "</body></html>";
		msg.setDataHandler(new DataHandler(new HTMLDataSource(html)));
	}
	
	static class HTMLDataSource implements DataSource {
		private String html;
		public HTMLDataSource(String htmlString) {
			html = htmlString;
		}
		
		public InputStream getInputStream() throws IOException {
			if (html == null) throw new IOException("Null HTML");
			return new ByteArrayInputStream(html.getBytes());
		}
		
		public OutputStream getOutputStream() throws IOException {
			throw new IOException("This DataHandler cannot write HTML");
		}
		
		public String getContentType() {
			return "text/html";
		}
		
		public String getName() {
			return "JAF text/html dataSource to send e-mail only";
		}
	}
	
}
